#ifndef QRCODEMANAGER_H
#define QRCODEMANAGER_H

#include <QObject>
#include "CameraManager.h"
#include "QZXing.h"
#include "QZXing_global.h"
#include "base/canbus.h"
#include "ServoManager.h"
#include "base/Command.h"
class QRCode
{
public:
    Rect ROI;
    QString text;
    Point centerPoint;
    QList<Rect> LocatorHole;

    void GetQRCodeROI()
    {
        ROI.x=LocatorHole[0].x-20;
        ROI.y=LocatorHole[0].y-20;
        ROI.height = LocatorHole[1].y+LocatorHole[1].height+20 - ROI.y;
        ROI.width = LocatorHole[2].x+LocatorHole[2].width+20 - ROI.x;
    }
    void GetCenterPoint_X()
    {
        centerPoint.x = LocatorHole[0].x+(LocatorHole[2].x-LocatorHole[0].x)*3/4;
    }
    void GetCenterPoint_Y()
    {
        centerPoint.y = LocatorHole[0].y+(LocatorHole[1].y-LocatorHole[0].y)*3/4;
    }
    int GetQR2CenterPxDistance_X(int frame_width_center)
    {
        GetCenterPoint_X();
        return frame_width_center - centerPoint.x;
    }

    int GetQR2CenterPxDistance_Y(int frame_height_center)
    {
        GetCenterPoint_Y();
        return frame_height_center - centerPoint.y;
    }

    int GetQRPxWidth()
    {
        return LocatorHole[2].x - LocatorHole[0].x;
    }

    int GetQRPxHeight()
    {
        return LocatorHole[1].y - LocatorHole[0].x;
    }

    int GetQR2LayerOffsetDistance_Y(int layeroffset)
    {
        GetCenterPoint_Y();
        return layeroffset - centerPoint.y;
    }
};

class QRCodeManager : public QObject
{
    Q_OBJECT
public:
    explicit QRCodeManager(QObject *parent = nullptr);
    ~QRCodeManager();

    const static int RealLocatorQRcode_mm = 28;//用于定位的二维码的三分之二宽度

    bool GetQRCodeFrame(Mat frame,QList<QRCode> &QRCodeFrame);
    bool detectQRcode(QList<Rect> &po);
    void GetQRCodeText_Single(Mat frame, QRCode &QRCodeFrame);
    void GetQRCodeText_Multi(Mat frame, QList<QRCode> &QRCodeFrame);
    bool GetLocatorDistance(uint8_t type, uint8_t targetLocator,QList<QRCode> QRCodeFrame,int &realdistance,int center);
    bool GetTargetArvQR(QList<QRCode> QRCodeFrame,QString text);
    QString DeCode_QZxing(Mat qrcode);
    QImage MatToImage(Mat &mat);
    int PxToMMConvert(float pxQRWidth,int pxQR2Center);
signals:
    void sig_FindLocatorDistance(bool,int);
    void sig_FindTargetArvQR(bool,int);
public slots:
    void slot_QRCodeInit();
    void slot_StartQRLocateScan(quint8 type,quint8 locatorNo);
    void slot_StartTargetArvQRScan();
private:
    CameraManager *pMyCapManager;
    QZXing *pQRDecoder;
};

#endif // QRCODEMANAGER_H
